#include "TimedTask.h"

// @author A0096955J

// this constructor creates a default DeadlineTask initalised with
// default values
TimedTask::TimedTask(){
	FloatingTask();

	setStartTimeAndDate(time(&startTimeAndDateTime_t));
	setEndTimeAndDate(time(&endTimeAndDateTime_t));

	startTimeAndDateTmFormat.tm_hour = CommonConstant::DEFAULT_TIME_HOURS;
	startTimeAndDateTmFormat.tm_min = CommonConstant::DEFAULT_TIME_MINUTES;
	startTimeAndDateTmFormat.tm_sec = CommonConstant::DEFAULT_TIME_SECONDS;
	setStartTimeAndDate(startTimeAndDateTmFormat);

	endTimeAndDateTmFormat.tm_hour = CommonConstant::DEFAULT_END_TIME_HOURS;
	endTimeAndDateTmFormat.tm_min = CommonConstant::DEFAULT_END_TIME_MINUTES;
	endTimeAndDateTmFormat.tm_sec = CommonConstant::DEFAULT_END_TIME_SECONDS;
	setEndTimeAndDate(endTimeAndDateTmFormat);

	taskType = CommonConstant::TASK_TYPE_TIMED;
}

TimedTask::TimedTask(DeadlineTask* curr){
	FloatingTask();

	setTitle(curr->getTitle());
	setDescription(curr->getDescription());
	setPerson(curr->getPerson());
	setPriority(curr->getPriority());
	setLocation(curr->getLocation());
	setIndex(curr->getIndex());
	setFinishedStatus(curr->getFinishedStatus());
	setStartTimeAndDate(curr->getTimeAndDateTmFormat());
	setEndTimeAndDate(time(&endTimeAndDateTime_t));

	endTimeAndDateTmFormat.tm_hour = CommonConstant::DEFAULT_END_TIME_HOURS;
	endTimeAndDateTmFormat.tm_min = CommonConstant::DEFAULT_END_TIME_MINUTES;
	endTimeAndDateTmFormat.tm_sec = CommonConstant::DEFAULT_END_TIME_SECONDS;
	setEndTimeAndDate(endTimeAndDateTmFormat);

	taskType = CommonConstant::TASK_TYPE_TIMED;
}

TimedTask::TimedTask(string newTitle,
					 string newDescription, 
					 string newLocation,
					 string newPerson,
					 int newPriority, 
					 int newFinishedStatus, 
					 int newIndex, 
					 time_t newStartTimeAndDate,
					 time_t newEndTimeAndDate){
	setTitle(newTitle);
	setDescription(newDescription);
	setLocation(newLocation);
	setPerson(newPerson);
	setPriority(newPriority);
	setFinishedStatus(newFinishedStatus);
	setIndex(newIndex);
	setStartTimeAndDate(newStartTimeAndDate);
	setEndTimeAndDate(newEndTimeAndDate);

	taskType = CommonConstant::TASK_TYPE_TIMED;
}

TimedTask* TimedTask::clone() const{
	return new TimedTask (*this);
}

string TimedTask::getStartTime(){
	strftime(CommonConstant::buffer,
			 CommonConstant::MAX_SIZE_TIME_STRING_FORMAT,
			 CommonConstant::TIME_24H_STRING_FORMAT.c_str(),
			 &startTimeAndDateTmFormat);

	return CommonConstant::buffer;
}

string TimedTask::getStartDate(){
	strftime(CommonConstant::buffer,
			 CommonConstant::MAX_SIZE_DATE_STRING_FORMAT,
			 CommonConstant::DATE_STRING_FORMAT.c_str(),
			 &startTimeAndDateTmFormat);

	return CommonConstant::buffer;
}

string TimedTask::getEndTime(){
	strftime(CommonConstant::buffer,
			 CommonConstant::MAX_SIZE_TIME_STRING_FORMAT,
			 CommonConstant::TIME_24H_STRING_FORMAT.c_str(),
			 &endTimeAndDateTmFormat);

	return CommonConstant::buffer;
}

string TimedTask::getEndDate(){
	strftime(CommonConstant::buffer,
			 CommonConstant::MAX_SIZE_DATE_STRING_FORMAT,
			 CommonConstant::DATE_STRING_FORMAT.c_str(),
			 &endTimeAndDateTmFormat);

	return CommonConstant::buffer;
}

string TimedTask::getFullStartDateInfo(){
	strftime(CommonConstant::day,
			 CommonConstant::MAX_SIZE_DAY_STRING_FORMAT,
			 CommonConstant::DAY_STRING_FORMAT.c_str(),
			 &startTimeAndDateTmFormat);
	strftime(CommonConstant::month,
			 CommonConstant::MAX_SIZE_MONTH_STRING_FORMAT,
			 CommonConstant::MONTH_STRING_FORMAT.c_str(),
			 &startTimeAndDateTmFormat);
	strftime(CommonConstant::year,
			 CommonConstant::MAX_SIZE_YEAR_STRING_FORMAT,
			 CommonConstant::YEAR_STRING_FORMAT.c_str(),
			 &startTimeAndDateTmFormat);

	sprintf_s(CommonConstant::buffer,
			  CommonConstant::FULL_DATE_INFO.c_str(),
			  CommonConstant::day,
			  to_string(startTimeAndDateTmFormat.tm_mday).c_str(),
			  CommonConstant::month,CommonConstant::year);

	return CommonConstant::buffer;
}

string TimedTask::getFullEndDateInfo(){
	strftime(CommonConstant::day,
			 CommonConstant::MAX_SIZE_DAY_STRING_FORMAT,
			 CommonConstant::DAY_STRING_FORMAT.c_str(),
			 &endTimeAndDateTmFormat);
	strftime(CommonConstant::month,
			 CommonConstant::MAX_SIZE_MONTH_STRING_FORMAT,
			 CommonConstant::MONTH_STRING_FORMAT.c_str(),
			 &endTimeAndDateTmFormat);
	strftime(CommonConstant::year,
			 CommonConstant::MAX_SIZE_YEAR_STRING_FORMAT,
			 CommonConstant::YEAR_STRING_FORMAT.c_str(),
			 &endTimeAndDateTmFormat);

	sprintf_s(CommonConstant::buffer,
			  CommonConstant::FULL_DATE_INFO.c_str(),
			  CommonConstant::day,
			  to_string(endTimeAndDateTmFormat.tm_mday).c_str(),
			  CommonConstant::month,CommonConstant::year);

	return CommonConstant::buffer;
}

time_t TimedTask::getStartTimeAndDate(){
	return startTimeAndDateTime_t;
}

time_t TimedTask::getEndTimeAndDate(){
	return endTimeAndDateTime_t;
}

struct tm TimedTask::getStartTimeAndDateTmFormat(){
	return startTimeAndDateTmFormat;
}

struct tm TimedTask::getEndTimeAndDateTmFormat(){
	return endTimeAndDateTmFormat;
}

void TimedTask::setStartTimeAndDate(time_t newTimeAndDate){
	startTimeAndDateTime_t = newTimeAndDate;
	localtime_s(&startTimeAndDateTmFormat,&startTimeAndDateTime_t);
}

void TimedTask::setStartTimeAndDate(struct tm newTimeAndDate){
	startTimeAndDateTmFormat = newTimeAndDate;
	startTimeAndDateTime_t = mktime(&startTimeAndDateTmFormat);
}

void TimedTask::setEndTimeAndDate(time_t newTimeAndDate){
	endTimeAndDateTime_t = newTimeAndDate;
	localtime_s(&endTimeAndDateTmFormat,&endTimeAndDateTime_t);
}

void TimedTask::setEndTimeAndDate(struct tm newTimeAndDate){
	endTimeAndDateTmFormat = newTimeAndDate;
	endTimeAndDateTime_t = mktime(&endTimeAndDateTmFormat);
}

string TimedTask::taskInfomation(){
	sprintf_s(CommonConstant::buffer,
			  CommonConstant::TIMED_TASK_STRING_FORMAT.c_str(),
			  FloatingTask::taskInfomation().c_str(),
			  getStartTime().c_str(),
			  getStartDate().c_str(),
			  getEndTime().c_str(),
			  getEndDate().c_str(),
			  CommonConstant::PATTERN.c_str());

	return CommonConstant::buffer;
}

string TimedTask::basicTaskInfomation(){
	sprintf_s(CommonConstant::buffer,
			  CommonConstant::TIMED_TASK_STRING_FORMAT.c_str(),
			  FloatingTask::basicTaskInfomation().c_str(),
			  getStartTime().c_str(),
			  getStartDate().c_str(),
			  getEndTime().c_str(),
			  getEndDate().c_str(),
			  CommonConstant::PATTERN.c_str());

	return CommonConstant::buffer;
}

TimedTask::~TimedTask(){
}